package vzaar

import scala.collection.jcl.Conversions._
import scala.collection.jcl.HashMap
import scala.collection.jcl.{MapWrapper => JMap}

import com.vzaar.{UploadProgressCallback=>JUploadProgressCallback}
import com.vzaar.transport.{VzaarTransport=>JTransport}
import com.vzaar.transport.{VzaarTransportFactory=>JTransportFactory}
import com.vzaar.transport.{VzaarTransportResponse=>JTransportResponse}

/**
 * Bridge for scala VzaarTransportFactory trait and java api VzaarTransportFactory interface.
 * This is so that java based transport layers can be used with the scala vzaar
 * library.
 * 
 * @param javaFactory an instance of java based VzaarTransportFactory
 * @author Marc G. Smith
 */
class JavaTransportFactoryBridge(javaFactory:JTransportFactory) extends VzaarTransportFactory {
	def create() = new JavaTransportBridge(javaFactory.create())
}

/**
 * Bridge for scala VzaarTransport trait and java VzaarTransport interface.
 * This is so that java based transport layers can be used with the scala vzaar
 * library.
 * 
 * @param javaTransport an instance of java based VzaarTransport
 * @author Marc G. Smith
 */
class JavaTransportBridge(javaTransport:JTransport) extends VzaarTransport {
	
  /**
   * Bridge the setUrl call between the scala trait and the java interface  
   */
  def setUrl(url:String) = javaTransport.setUrl(url)
	
  /**
   * Bridge the sendGetRequest call between the scala trait and the java interface  
   */
  def sendGetRequest(uri:String, parameters:Map[String,String]): VzaarTransportResponse = 
	transformResponse(javaTransport.sendGetRequest(uri, transformMap(parameters)));
	
  /**
   * Bridge the sendPostRequest call between the scala trait and the java interface
   */
  def sendPostRequest(uri:String, parameters:Map[String,String]): VzaarTransportResponse = 
	transformResponse(javaTransport.sendPostRequest(uri, transformMap(parameters)));
  
  /**
   * Bridge the sendPostXmlRequest call between the scala trait and the java interface  
   */
  def sendPostXmlRequest(uri:String, xml:String): VzaarTransportResponse = 
	transformResponse(javaTransport.sendPostXmlRequest(uri, xml))
 
  /**
   * Bridge the setOAuthTokens call between the scala trait and the java interface  
   */
  def setOAuthTokens(token:String, secret:String) = 
	javaTransport.setOAuthTokens(token, secret)
 
  /**
   * Bridge the uploadToS3 call between the scala trait and the java interface  
   */
  def uploadToS3(
	url:String, parameters:Map[String, String], file:java.io.File, 
    callback:UploadProgressCallback): VzaarTransportResponse = 
	transformResponse(javaTransport.uploadToS3(url, transformMap(parameters), file, new UploadProgressCallbackConverter(callback)))
	
  /**
   * Bridge the setDebugStream call between the scala trait and the java interface  
   */
  def setDebugStream(out:java.io.OutputStream) = javaTransport.setDebugStream(out)
 
  /**
   * Transform a java transport response to a scala transport response
   */
  private def transformResponse(javaResponse: JTransportResponse): VzaarTransportResponse = 
	new VzaarTransportResponse(
	  javaResponse.getStatusCode(),
	  javaResponse.getStatusLine(),
	  javaResponse.getResponse())
 
  /**
   * Transform a parameter map from scala to a native java map
   */
  private def transformMap(map:Map[String,String]):JMap[String,String] = {
	var tm = new HashMap[String,String]
	if(map != null) tm ++= map
	return tm
  }
}

private class UploadProgressCallbackConverter(delegate:UploadProgressCallback) extends JUploadProgressCallback {
  	def error(file:java.io.File, error:String) = 
  	  delegate.error(file.getAbsolutePath, error)
	
    def progress(file: java.io.File, sent:Long, length: Long) = 
  	  delegate.progress(file.getAbsolutePath, sent, length)
	
    def complete(file: java.io.File, videoId:Int) = 
	  delegate.complete(file.getAbsolutePath, videoId.toString)
}
